#include "StdAfx.h"

//tile drawers from my old breem :)
void drawtile_8x8(u8 *dest,int p,int tilenum,u8 x,u8 y,u8 *b[])
{
u8 *s = dest + x + (y * p);
u32 adder = tilenum * 8;
int i,j;
u8 b0,b1,b2;
u8 *bb[3];

bb[0] = b[0] + adder;
bb[1] = b[1] + adder;
bb[2] = b[2] + adder;
for(j=0;j<8;j++)
	{
	b0 = *bb[0]++;
	b1 = *bb[1]++;
	b2 = *bb[2]++;
	for(i=8-1;i>=0;i--)
		{
		u8 color = (u8)(((b0 >> i) & 1) | (((b1 >> i) & 1) << 1) | (((b2 >> i) & 1) << 2));

		if(color)
			*s++ = color;
		else
			s++;
		}
	s += p - 8;
	}
}

void drawtile_8x8_or(u8 *dest,int p,int tilenum,u8 x,u8 y,u8 *b[],u8 oor)
{
u8 *s = dest + x + (y * p);
u32 adder = tilenum * 8;
int i,j;
u8 b0,b1,b2;
u8 *bb[3];

bb[0] = b[0] + adder;
bb[1] = b[1] + adder;
bb[2] = b[2] + adder;
for(j=0;j<8;j++)
	{
	b0 = *bb[0]++;
	b1 = *bb[1]++;
	b2 = *bb[2]++;
	for(i=8-1;i>=0;i--)
		{
		u8 color = (u8)(((b0 >> i) & 1) | (((b1 >> i) & 1) << 1) | (((b2 >> i) & 1) << 2));

		if(color)
			*s++ = color | oor;
		else
			s++;
		}
	s += p - 8;
	}
}

void drawtile_16x16(u8 *dest,int p,int tilenum,u8 x,u8 y,u8 *b[])
{
u8 *s = dest + x + (y * p);
u32 adder = tilenum * 32;
int i,j;
u16 b0,b1,b2;
u8*bb[3];

bb[0] = b[0] + adder;
bb[1] = b[1] + adder;
bb[2] = b[2] + adder;
for(j=0;j<16;j++)
	{
	b0 = (u16)(*bb[0] | ((*(bb[0] + 16)) << 8));
	b1 = (u16)(*bb[1] | ((*(bb[1] + 16)) << 8));
	b2 = (u16)(*bb[2] | ((*(bb[2] + 16)) << 8));
	bb[0]++;
	bb[1]++;
	bb[2]++;
	for(i=16-1;i>=0;i--)
		{
		u8 color = (u8)(((b0 >> i) & 1) | (((b1 >> i) & 1) << 1) | (((b2 >> i) & 1) << 2));

		if(color)
			*s++ = color;
		else
			s++;
		}
	s += p - 16;
	}
}

void drawtile_16x16_flipx(u8 *dest,int p,int tilenum,u8 x,u8 y,u8 *b[])
{
u8 *s = dest + x + (y * p);
u32 adder = tilenum * 32;
int i,j;
u16 b0,b1,b2;
u8*bb[3];

bb[0] = b[0] + adder;
bb[1] = b[1] + adder;
bb[2] = b[2] + adder;
for(j=0;j<16;j++)
	{
	b0 = (u16)(*bb[0] | ((*(bb[0] + 16)) << 8));
	b1 = (u16)(*bb[1] | ((*(bb[1] + 16)) << 8));
	b2 = (u16)(*bb[2] | ((*(bb[2] + 16)) << 8));
	bb[0]++;
	bb[1]++;
	bb[2]++;
	for(i=0;i<16;i++)
		{
		u8 color = (u8)(((b0 >> i) & 1) | (((b1 >> i) & 1) << 1) | (((b2 >> i) & 1) << 2));

		if(color)
			*s++ = color;
		else
			s++;
		}
	s += p - 16;
	}
}

void drawtile_16x16_flipy(u8 *dest,int p,int tilenum,u8 x,u8 y,u8 *b[])
{
u8 *s = dest + x + (y * p);
u32 adder = tilenum * 32;
int i,j;
u16 b0,b1,b2;
u8 *bb[3];

bb[0] = b[0] + adder + 15;
bb[1] = b[1] + adder + 15;
bb[2] = b[2] + adder + 15;
for(j=0;j<16;j++)
	{
	b0 = (u16)(*bb[0] | ((*(bb[0] + 16)) << 8));
	b1 = (u16)(*bb[1] | ((*(bb[1] + 16)) << 8));
	b2 = (u16)(*bb[2] | ((*(bb[2] + 16)) << 8));
	bb[0]--;
	bb[1]--;
	bb[2]--;
	for(i=16-1;i>=0;i--)
		{
		u8 color = (u8)(((b0 >> i) & 1) | (((b1 >> i) & 1) << 1) | (((b2 >> i) & 1) << 2));

		if(color)
			*s++ = color;
		else
			s++;
		}
	s += p - 16;
	}
}

void drawtile_16x16_flipxy(u8 *dest,int p,int tilenum,u8 x,u8 y,u8 *b[])
{
u8 *s = dest + x + (y * p);
u32 adder = tilenum * 32;
int i,j;
u16 b0,b1,b2;
u8*bb[3];

bb[0] = b[0] + adder + 15;
bb[1] = b[1] + adder + 15;
bb[2] = b[2] + adder + 15;
for(j=0;j<16;j++)
	{
	b0 = (u16)(*bb[0] | ((*(bb[0] + 16)) << 8));
	b1 = (u16)(*bb[1] | ((*(bb[1] + 16)) << 8));
	b2 = (u16)(*bb[2] | ((*(bb[2] + 16)) << 8));
	bb[0]--;
	bb[1]--;
	bb[2]--;
	for(i=0;i<16;i++)
		{
		u8 color = (u8)(((b0 >> i) & 1) | (((b1 >> i) & 1) << 1) | (((b2 >> i) & 1) << 2));

		if(color)
			*s++ = color;
		else
			s++;
		}
	s += p - 16;
	}
}

void drawtile_16x16_or_notrans(u8 *dest,int p,int tilenum,u8 x,u8 y,u8 *b[],u8 oor)
{
u8 *s = dest + x + (y * p);
u32 adder = tilenum * 32;
int i,j;
u16 b0,b1,b2;
u8 *bb[3];

bb[0] = b[0] + adder;
bb[1] = b[1] + adder;
bb[2] = b[2] + adder;
for(j=0;j<16;j++)
	{
	b0 = (u16)(*bb[0] | ((*(bb[0] + 16)) << 8));
	b1 = (u16)(*bb[1] | ((*(bb[1] + 16)) << 8));
	b2 = (u16)(*bb[2] | ((*(bb[2] + 16)) << 8));
	bb[0]++;
	bb[1]++;
	bb[2]++;
	for(i=16-1;i>=0;i--)
		*s++ = ((((b0 >> i) & 1) | (((b1 >> i) & 1) << 1) | (((b2 >> i) & 1) << 2)) | oor);
	s += p - 16;
	}
}

void drawtile_16x16_or_notrans_bnj(u8 *dest,int p,int tilenum,int x,int y,u8 *b[],u8 oor)
{
u8 *s = dest + x + (y * p);
u32 adder = tilenum * 64;
int j;
u16 b0,b1,b2;
u8 *bb[3];

#define drawpixels()\
	*s++ = ((((b0 >> 3) & 1) | (((b1 >> 3) & 1) << 1) | (((b2 >> 3) & 1) << 2)) | oor);\
	*s++ = ((((b0 >> 2) & 1) | (((b1 >> 2) & 1) << 1) | (((b2 >> 2) & 1) << 2)) | oor);\
	*s++ = ((((b0 >> 1) & 1) | (((b1 >> 1) & 1) << 1) | (((b2 >> 1) & 1) << 2)) | oor);\
	*s++ = ((((b0 >> 0) & 1) | (((b1 >> 0) & 1) << 1) | (((b2 >> 0) & 1) << 2)) | oor);
bb[0] = b[0] + adder;
bb[1] = b[1] + adder;
bb[2] = b[2] + adder;
for(j=0;j<16;j++)
	{
	b0 = bb[0][48];	b1 = bb[1][48]>>4;	b2 = bb[2][48];	drawpixels();
	b0 = bb[0][32];	b1 = bb[1][32]>>4;	b2 = bb[2][32];	drawpixels();
	b0 = bb[0][16];	b1 = bb[1][16]>>4;	b2 = bb[2][16];	drawpixels();
	b0 = bb[0][0];		b1 = bb[1][0]>>4;		b2 = bb[2][0];		drawpixels();
	bb[0]++;				bb[1]++;					bb[2]++;
	s += p - 16;
	bb[0] = b[0] + adder + j;
	bb[1] = b[1] + adder + j;
	bb[2] = b[2] + adder + j;
	}
}
